import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'knowledge_vm.dart';

class KnowledgeDetailChild extends StatefulWidget {
  final int cid;
  const KnowledgeDetailChild({super.key, required this.cid});

  @override
  State<KnowledgeDetailChild> createState() => _KnowledgeDetailState();
}

class _KnowledgeDetailState extends State<KnowledgeDetailChild> {
  KnowledgeViewModel knowledgeViewModel = KnowledgeViewModel();
  RefreshController refreshController = RefreshController();

  @override
  void initState() {
    super.initState();
    knowledgeViewModel.getKnowledgeDetail(id: widget.cid);
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider.value(
      value: knowledgeViewModel,
      child: SmartRefresher(
        controller: refreshController,
        onRefresh: () async {
          print('onRefresh');
          // await knowledgeViewModel.getKnowledgeDetail(id: widget.cid);
          refreshController.refreshCompleted();
        },
        onLoading: () async {
          print('onLoading');
          await knowledgeViewModel.getKnowledgeDetail(
            id: widget.cid,
            isMore: true,
          );
          refreshController.loadComplete();
        },
        header: ClassicHeader(),
        footer: ClassicFooter(),
        enablePullDown: true,
        enablePullUp: true,
        child: Consumer<KnowledgeViewModel>(
          builder: (context, vm, child) {
            return ListView.builder(
              shrinkWrap: true,
              physics: NeverScrollableScrollPhysics(),
              itemCount: vm.knowledgeDetail?.length ?? 0,
              itemBuilder: (context, index) {
                return _buildItem(vm.knowledgeDetail?[index]);
              },
            );
          },
        ),
      ),
    );
  }

  _buildItem(item) {
    return Container(
      margin: EdgeInsets.all(10.0),
      padding: EdgeInsets.all(10.0),
      decoration: BoxDecoration(
        border: Border.all(color: Colors.grey[300]!),
        borderRadius: BorderRadius.circular(8.0),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Text(
                item.chapterName ?? '',
                style: TextStyle(fontSize: 16.0, fontWeight: FontWeight.bold),
              ),
              Spacer(),
              Text(
                item.niceDate ?? '',
                style: TextStyle(fontSize: 14.0, color: Colors.grey),
              ),
            ],
          ),
          SizedBox(height: 5.0),
          Text(
            item.title ?? '',
            style: TextStyle(fontSize: 16.0, fontWeight: FontWeight.bold),
          ),
          SizedBox(height: 5.0),
          Row(
            children: [
              Text(
                item.superChapterName ?? '',
                style: TextStyle(fontSize: 14.0, color: Colors.grey),
              ),
              Spacer(),
              Text(
                item.shareUser ?? '',
                style: TextStyle(fontSize: 14.0, color: Colors.grey),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
